<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\OpenCity;
use App\Models\RandAd;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class RandAdController extends Controller
{
    use HasResourceActions;
    private $name = '随机广告';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new RandAd);

        $grid->id('Id');
        $grid->name('名称');
        $grid->adurl('广告图')->image('', 100, 30);
        $grid->City()->name('地区');
        $grid->sdate('开始日期');
        $grid->edate('结束日期');
        $grid->stime('开始时间');
        $grid->etime('结束时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(RandAd::findOrFail($id));

        $show->id('Id');
        $show->name('名称');
        $show->adurl('广告图');
        $show->location('地区');
        $show->sdate('开始日期');
        $show->edate('结束日期');
        $show->stime('开始时间');
        $show->etime('结束时间');
        $show->sort('排序');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new RandAd);

        $cityList = OpenCity::getCityList();
        $cityList['000000'] = '全国';

        $form->text('name', '名称')->rules('required');
        $form->image('adurl', '广告图');
        $form->select('location', '地区')->options($cityList)->rules('required');
        $form->date('sdate', '开始日期')->default(date('Y-m-d'))->rules('required');
        $form->date('edate', '结束日期')->default(date('Y-m-d'))->rules('required');
        $form->time('stime', '开始时间')->default(date('H:i:s'))->rules('required');
        $form->time('etime', '结束日期')->default(date('H:i:s'))->rules('required');

        return $form;
    }
}
